﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace DotNETX.Utility.Reflection
{

    public static class AssemblyUtils
    {
        public static string GetCompleteProductName(Assembly assembly)
        {
            return GetProductName(assembly) + " " + FormatVersion(assembly.GetName().Version);
        }

        public static string GetProductName(Assembly assembly)
        {
            object[] attrs = (assembly as ICustomAttributeProvider).GetCustomAttributes(typeof(AssemblyProductAttribute), true);
            if (attrs == null || attrs.Length == 0 || attrs[0] == null || !(attrs[0] is AssemblyProductAttribute)) return null;
            AssemblyProductAttribute productAttribute = (attrs[0] as AssemblyProductAttribute);

            return productAttribute.Product;
        }

        public static string FormatVersion(Version assemblyVersion)
        {
            if (assemblyVersion.Build == 0)
                return assemblyVersion.Major + "." + assemblyVersion.Minor;
            else
                return assemblyVersion.Major + "." + assemblyVersion.Minor + "." + assemblyVersion.Build;
        }

        public static string GetCopyright(Assembly assembly)
        {
            object[] attrs = (assembly as ICustomAttributeProvider).GetCustomAttributes(typeof(AssemblyCopyrightAttribute), true);
            if (attrs == null || attrs.Length == 0 || attrs[0] == null || !(attrs[0] is AssemblyCopyrightAttribute)) return null;
            AssemblyCopyrightAttribute copyrightAttribute = (attrs[0] as AssemblyCopyrightAttribute);

            return copyrightAttribute.Copyright;
        }
    }

}
